module PNEngine
  class Locator < ActiveRecord::Base
    belongs_to :place
    validates_inclusion_of :val_type, :in => ["xsd:date", "xsd:dateTime", "xsd:anyURI", "xsd:string", "xsd:integer", "xsd:decimal", "xsd:double", "xsd:float", "xsd:boolean"], :message => "VALIDATION/LOCATOR: No valid val_type"
    
	def write(xml, val)
    XML.write_xpath(xml, expr, val) 
	end
	
    # evaluates given xml for token
    def evaluate(xml)
      val = XML.read_xpath(xml, expr)
      
      case val_type
      when "xsd:string"
        val.to_s
      when "xsd:integer"
        val.to_i
      when "xsd:double"
        val.to_f
      when "xsd:date"
        val = Date.parse(val)
      when "xsd:dateTime"
        val = DateTime.parse(val)
      when "xsd:anyURI"
        val = URI.parse(val)
      when "xsd:decimal"
        val.to_f
      when "xsd:float"
        val.to_f
      when "xsd:boolean"
        if val == "true"
          true
        elsif val == "false"
          false
        else
          nil
        end
      else
        raise "Unknown Type #{val_type}"
      end
    end
  end
end
